<?php
include 'connect.php';

$error = "";

if (isset($_POST["title"])) {
	$image = "";
	if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png"))
	{
	  if ($_FILES["file"]["error"] > 0) {
		$error = "Return Code: " . $_FILES["file"]["error"];
		
		//echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
	  } else {
			//echo "Upload: " . $_FILES["file"]["name"] . "<br />";
			//echo "Type: " . $_FILES["file"]["type"] . "<br />";
			//echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
			//echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

			/*if (file_exists("upload/" . $_FILES["file"]["name"]))
			{
				//echo $_FILES["file"]["name"] . " already exists. ";
				$error = $_FILES["file"]["name"] . " already exists. ";
		
			} else {*/
				  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
				  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
				  $image = "upload/" . $_FILES["file"]["name"];
				  //$check['file'] = $_FILES["file"]["name"] . " already exists. ";
				  //$valid = FALSE;
			//}
		}	
	} else {
		$error = "Invalid file";
		
	}
	
	$param = "(";
		$param .= "'" . (isset($_POST["title"]) ? $_POST["title"] : "") . "'";
		$param .= ", '" . (isset($_POST["content"]) ? $_POST["content"] : "") . "'";
		$param .= ", " . (isset($_POST["price"]) ? $_POST["price"] : "") . 0;
		$param .= ", " . (isset($_POST["category"]) ? $_POST["category"] : 0) . "";
		$param .= ", " . (isset($_POST["popularity"]) ? $_POST["popularity"] : 0) . "";
		$param .= ", " . (isset($_POST["rating_min"]) ? $_POST["rating_min"] : 0) . "";
		$param .= ", " . (isset($_POST["rating_max"]) ? $_POST["rating_max"] : 0) . "";
		$param .= ", '" . $image . "'";
		$param .= ")";
	
		$query = "INSERT INTO item (title, content, price, category, popularity, rating_min, rating_max, image) VALUES " . $param . "";
		mysql_query($query)  or die(mysql_error()); 
}

$data_category = mysql_query("SELECT * FROM category")  or die(mysql_error()); 
$data_item = mysql_query("SELECT item.id, title, content, price, image, popularity, category.name, rating_min, rating_max  FROM item INNER JOIN category ON item.category = category.id")  or die(mysql_error()); 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ASMO-708">
<link href="css/style_demo.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<title>New Site Domain Title</title>
</head>
<body>
<div id="container">
	<div id="main">
	<?=$error ?>
		<div class="submit-form">
			<form action="add_item.php" method="post" enctype="multipart/form-data">
			<table width="100%" cellpadding="0" cellspacing="0">
				<thead>
					<tr>
						<td colspan="2"><div class="title-form">Add Form</div></td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td width="30%">Title</td>
						<td><input type="text" name="title" value=""></td>
					</tr>
					<tr>
						<td width="30%" class="content">Content</td>
						<td>
							<textarea name="content"></textarea>
						</td>
					</tr>
					<tr>
						<td width="30%" class="price">Price</td>
						<td><input type="text" name="price" value=""></td>
					</tr>
					<tr>
						<td width="30%" class="rating_min">Rating Min</td>
						<td><input type="text" name="rating_min" value=""></td>
					</tr>
					<tr>
						<td width="30%" class="rating_max">Rating Max</td>
						<td><input type="text" name="rating_max" value=""></td>
					</tr>
					<tr>
						<td width="30%" class="popularity">Popularity</td>
						<td>
							<input type="text" name="popularity" value="">
						</td>
					</tr>
					<tr>
						<td width="30%" class="image">Image</td>
						<td><input type="file" name="file" id="image" /> </td>
					</tr>
					<tr>
						<td width="30%" class="category">Category</td>
						<td>
							<select name="category">
							<?php
							while($category = mysql_fetch_array( $data_category )) 
							{ 
							?>
								<option value="<?=$category['id']?>"><?=$category['name']?></option>
							<? } ?>
							</select>
						</td>
					</tr>
					<tr class="dotline">
						<td></td>
						<td>
							<button type="submit">Add</button>
						</td>
					</tr>
				</tbody>
			</table>
			</form>
		</div>
		<br />
		<div class="submit-form">
			<table width="100%" cellpadding="0" cellspacing="0">
				<thead>
					<tr>
						<td colspan="5"><div class="title-form">List item</div></td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Id</td>
						<td>title</td>
						<td>description</td>
						<td>Image</td>
						<td>Category</td>
					</tr>
					<?php
					while($item = mysql_fetch_array( $data_item )) 
					{ 
					?>
					<tr class="dotline">
						<td width="5%" class="name"><?=$item['id']?></td>
						<td><?=$item['title']?></td>
						<td><?=$item['content']?></td>
						<td><img src="<?=$item['image']?>" height="30"/></td>
						<td><?=$item['name']?></td>
					</tr>
					<?php
					}
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
</body>
</html>